﻿using System;
using System.ComponentModel.DataAnnotations;
using ReadingList.Domain.DomainObjects;

namespace ReadingList.Domain.ViewModels
{
    [MetadataType(typeof(UserBookViewModel))]
    public class UserBookViewModel
    {
        public UserBookViewModel(User user, Book book) {
            if (user == null)
                throw new ArgumentNullException("user");
            if (book == null)
                throw new ArgumentNullException("book");

            UserVm = MapUser(user);
            BookVm = MapBook(book);
        }

        [Required]
        public UserViewModel UserVm { get; private set; }

        [Required]
        public BookViewModel BookVm { get; private set; }

        [Required(ErrorMessage = "This book must have a topic.")]
        public string Topic { get; set; }

        public class BookViewModel
        {
            [Required]
            public string Title { get; set; }
        }

        public class UserViewModel
        {
            [Required]
            public string Username { get; set; }
        }

        private Func<User, UserViewModel> MapUser = result => {
            var user = new UserViewModel
                           {
                               Username = result.Username
                           };
            return user;
        };

        private Func<Book, BookViewModel> MapBook = result => {
            var book = new BookViewModel
            {
                Title = result.Title
            };
            return book;
        };
    }
}
